//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.anji.plus.gaea.curd.controller;

import com.anji.plus.gaea.annotation.DetailView;
import com.anji.plus.gaea.annotation.Permission;
import com.anji.plus.gaea.annotation.log.GaeaAuditLog;
import com.anji.plus.gaea.bean.ResponseBean;
import com.anji.plus.gaea.curd.dto.BaseDTO;
import com.anji.plus.gaea.curd.entity.GaeaBaseEntity;
import com.anji.plus.gaea.curd.params.PageParam;
import com.anji.plus.gaea.curd.service.GaeaBaseService;
import com.anji.plus.gaea.holder.UserContentHolder;
import com.anji.plus.gaea.utils.GaeaBeanUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

public abstract class GaeaBaseController<P extends PageParam, T extends GaeaBaseEntity, D extends BaseDTO> extends BaseResponse {
    public GaeaBaseController() {
    }

    public abstract GaeaBaseService<P, T> getService();

    public abstract T getEntity();

    public abstract D getDTO();

    @GetMapping({"/pageList"})
    @Permission(
            code = "query",
            name = "查询"
    )
    @GaeaAuditLog(
            pageTitle = "查询",
            isSaveResponseData = false
    )
    public ResponseBean pageList(P param) {
        IPage<T> iPage = this.getService().page(param);
        List<T> records = iPage.getRecords();

        List<D> list = (List<D>) GaeaBeanUtils.copyList(records, this.getDTO().getClass());
        this.pageResultHandler(list);
        Page<D> pageDto = new Page();
        pageDto.setCurrent(iPage.getCurrent()).setRecords(list).setPages(iPage.getPages()).setTotal(iPage.getTotal()).setSize(iPage.getSize());
        return this.responseSuccessWithData(pageDto);
    }

    public List<D> pageResultHandler(List<D> list) {
        return list;
    }

    public D detailResultHandler(D detail) {
        return detail;
    }

    @GetMapping({"/{id}"})
    @Permission(
            code = "detail",
            name = "明细"
    )
    
    @DetailView
    public ResponseBean detail(@PathVariable("id") String id) {
        T result = this.getService().selectOne(id);
        D dto = this.getDTO();
        GaeaBeanUtils.copyAndFormatter(result, dto);
        this.detailResultHandler(dto);
        ResponseBean responseBean = this.responseSuccessWithData(this.resultDtoHandle(dto));
        this.logger.info("根据ID查询结束，req：{},res:{}", id, responseBean);
        return responseBean;
    }

    protected D resultDtoHandle(D d) {
        return d;
    }

    @PostMapping
    @Permission(
            code = "insert",
            name = "新增"
    )
    @GaeaAuditLog(
            pageTitle = "新增"
    )
    public ResponseBean insert(@Validated @RequestBody D dto) {
        T entity = this.getEntity();
        entity.setCreateTime(new Date());
        String operator = UserContentHolder.getContext().getUsername();
        entity.setCreateBy(operator);

        BeanUtils.copyProperties(dto, entity);
        this.getService().insert(entity);
        ResponseBean ret = this.responseSuccessWithData(entity.copyTo(dto.getClass()));
        this.logger.info("新增服务结束，req：{},res:{}", dto, ret);
        return ret;
    }

    @PutMapping
    @Permission(
            code = "update",
            name = "修改"
    )
    @GaeaAuditLog(
            pageTitle = "修改"
    )
    public ResponseBean update(@Validated @RequestBody D dto) {
        String username = UserContentHolder.getContext().getUsername();
        T entity = this.getEntity();
        entity.setUpdateTime(new Date());
        entity.setUpdateBy(username);
        BeanUtils.copyProperties(dto, entity);
        this.getService().update(entity);
        this.logger.info("更新服务结束，user:{},req:{},res:{}", new Object[]{username, dto, entity});
        ResponseBean ret = this.responseSuccessWithData(entity.copyTo(dto.getClass()));
        return ret;
    }

    @DeleteMapping({"/{id}"})
    @Permission(
            code = "delete",
            name = "删除"
    )
    @GaeaAuditLog(
            pageTitle = "删除"
    )
    public ResponseBean deleteById(@PathVariable("id") String id) {
        this.getService().deleteById(id);
        this.logger.info("删除服务结束:{}", id);
        return this.responseSuccess();
    }

    @PostMapping({"/delete/batch"})
    @Permission(
            code = "deleteBatch",
            name = "批量删除"
    )
    @GaeaAuditLog(
            pageTitle = "批量删除"
    )
    public ResponseBean deleteBatchIds(@RequestBody List<Serializable> ids) {
        boolean deleteCount = this.getService().deleteByIds(ids);
        ResponseBean responseBean = this.responseSuccessWithData(deleteCount);
        this.logger.info("批量删除服务结束，req:{},ret：{}", ids, responseBean);
        return responseBean;
    }
}
